package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.DbUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 课程管理Service层实现类
 */
public class CourseContentServiceImp implements CourseContentService {

    CourseContentDao contentDao = new CourseContentDaoImpl();

    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        List<Course_Section> sections = contentDao.findSectionAndLessonByCourseId(courseId);

        return sections;
    }

    @Override
    public Course findCourseByCourseId(int courseId) {
        Course course = contentDao.findCourseByCourseId(courseId);
        return course;
    }

    @Override
    public String saveSection(Course_Section section) {
        //1、补全章节信息
        section.setStatus(2);  //状态 0-隐藏 1-待更新 2-已发布

        String dateFormart = DateUtils.getDateFormart();
        section.setCreate_time(dateFormart);
        section.setUpdate_time(dateFormart);

        //2、调用Dao
        int row = contentDao.saveSection(section);

        //3、根据是否插入成功,封装对应的信息
        if(row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateSection(Course_Section section) {

        //1、补全信息
        String dateFormart = DateUtils.getDateFormart();

        section.setUpdate_time(dateFormart);

        //2、调用dao
        int i = contentDao.updateSection(section);

        //3、判断是否插入成功
        if(i > 0){
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateSectionStatus(int id, int status) {

        int i = contentDao.updateSectionStatus(id, status);

        //判断是否修改成功
        if(i>0){
            //修改状态成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String saveLesson(Course_Lesson course_lesson) {
        //补全课时信息
        course_lesson.setStatus(2); //状态 0-隐藏 1-待更新 2-已发布
        String formart = DateUtils.getDateFormart();
        course_lesson.setCreate_time(formart);
        course_lesson.setUpdate_time(formart);

        int i = contentDao.saveLesson(course_lesson);
        //判断是否插入成功,封装对应的信息
        if(i > 0){
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            String result = StatusCode.FAIL.toString();
            return result;
        }

    }

    @Override
    public String updateLesson(Course_Lesson lesson) {
       //补全信息
        String formart = DateUtils.getDateFormart();
        lesson.setUpdate_time(formart);

        //调用Dao
        int i = contentDao.updateLesson(lesson);
        //判断是否插入成功,封装对应的信息
        if(i > 0){
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            String result = StatusCode.FAIL.toString();
            return result;
        }

    }


}
